#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "student.h"

// 使用数组存储学生信息（简化版，最多 100 个学生）
Student students[100];
int studentCount = 0;

/*
 * 初始化学生系统
 * 这里可以从文件中读取已有学生数据
 */
void initStudentSystem() {
    loadStudentsFromFile("students.txt");
}

/*
 * 添加学生信息
 * 输入学号、姓名、成绩，存入数组
 */
void addStudent() {
    if (studentCount >= 100) {
        printf("学生数量已满，无法添加！\n");
        return;
    }

    Student s;
    printf("请输入学号: ");
    scanf("%s", s.id);
    printf("请输入姓名: ");
    scanf("%s", s.name);
    printf("请输入成绩: ");
    scanf("%f", &s.score);

    students[studentCount++] = s;
    printf("学生添加成功！\n");
}

/*
 * 查询学生信息
 * 根据学号查找
 */
void queryStudent() {
    char id[20];
    printf("请输入要查询的学号: ");
    scanf("%s", id);

    for (int i = 0; i < studentCount; i++) {
        if (strcmp(students[i].id, id) == 0) {
            printf("学号: %s, 姓名: %s, 成绩: %.2f\n",
                   students[i].id, students[i].name, students[i].score);
            return;
        }
    }
    printf("未找到该学号的学生！\n");
}

/*
 * 修改学生成绩
 */
void modifyStudent() {
    char id[20];
    printf("请输入要修改的学号: ");
    scanf("%s", id);

    for (int i = 0; i < studentCount; i++) {
        if (strcmp(students[i].id, id) == 0) {
            printf("请输入新成绩: ");
            scanf("%f", &students[i].score);
            printf("成绩修改成功！\n");
            return;
        }
    }
    printf("未找到该学号的学生！\n");
}

/*
 * 删除学生信息
 * 用后面的覆盖当前，减少数组长度
 */
void deleteStudent() {
    char id[20];
    printf("请输入要删除的学号: ");
    scanf("%s", id);

    for (int i = 0; i < studentCount; i++) {
        if (strcmp(students[i].id, id) == 0) {
            // 将后面的元素前移
            for (int j = i; j < studentCount - 1; j++) {
                students[j] = students[j + 1];
            }
            studentCount--;
            printf("学生删除成功！\n");
            return;
        }
    }
    printf("未找到该学号的学生！\n");
}

/*
 * 统计成绩
 * 输出最高分、最低分、平均分
 */
void showStatistics() {
    if (studentCount == 0) {
        printf("暂无学生数据！\n");
        return;
    }

    float max = students[0].score;
    float min = students[0].score;
    float sum = 0;

    for (int i = 0; i < studentCount; i++) {
        if (students[i].score > max) max = students[i].score;
        if (students[i].score < min) min = students[i].score;
        sum += students[i].score;
    }

    printf("最高分: %.2f, 最低分: %.2f, 平均分: %.2f\n",
           max, min, sum / studentCount);
}

/*
 * 成绩排序
 * 简单冒泡排序，按成绩降序排列
 */
void sortStudents() {
    for (int i = 0; i < studentCount - 1; i++) {
        for (int j = 0; j < studentCount - i - 1; j++) {
            if (students[j].score < students[j + 1].score) {
                Student temp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = temp;
            }
        }
    }
    printf("成绩排序完成！\n");

    for (int i = 0; i < studentCount; i++) {
        printf("%s\t%s\t%.2f\n", students[i].id, students[i].name, students[i].score);
    }
}

/*
 * 保存学生数据到文件
 */
void saveStudentsToFile(const char* filename) {
    FILE* fp = fopen(filename, "w");
    if (fp == NULL) {
        printf("无法打开文件保存数据！\n");
        return;
    }
    for (int i = 0; i < studentCount; i++) {
        fprintf(fp, "%s %s %.2f\n", students[i].id, students[i].name, students[i].score);
    }
    fclose(fp);
    printf("数据保存成功！\n");
}

/*
 * 从文件加载学生数据
 */
void loadStudentsFromFile(const char* filename) {
    FILE* fp = fopen(filename, "r");
    if (!fp) return; // 如果文件不存在，直接返回

    while (fscanf(fp, "%s %s %f", students[studentCount].id,
                  students[studentCount].name,
                  &students[studentCount].score) == 3) {
        studentCount++;
    }
    fclose(fp);
}